# author: Han Zhang
##########################################################
######################## right panel of Figure 9 #################################

## this plot is for the validation data 
## it plots the precison/recall curve for the classifiers (1 and 2) combined together
## and the comparison between two-stage and one-stage classifier


library(ggplot2)


DPI = 300
WIDTH = 7
UNIT = "in"






# df2 <- read.csv("c1c2.csv", sep = ',')
df2 <- read.csv("secondstage_c2_text_image_precision_recall.csv", sep = ',')

df2$f1 = with(df2, 2 * precision * recall /(precision + recall))

df2 = df2[df2$label %in% c("C2, text and image", "C1, text and image"), ]


## levels of factors for scaling
lev = c('C2, text and image', 'C1, text and image')
df2$label = factor(df2$label, levels = lev)


##### c1 vs c2
df2$label = as.character(df2$label)
df2$precision = as.numeric(df2$precision)
df2$recall = as.character(df2$recall)

df2$label [df2$label == "C2, text and image" ] <- "Two-stage Classifier" 
df2$label [df2$label == "C1, text and image" ] <- "One-stage Classifier" 


df2 = rbind (df2, c ("Random Guess", 0.3862, 0, 0.5 ))
df2 = rbind (df2, c ("Random Guess", 0.3862, 0, 0.5, 0.5 ))
df2 = rbind (df2, c ("Random Guess", 0.3862, 1, 0.5 ))

df2$label = as.character(df2$label)
df2$precision = as.numeric(df2$precision)

df2$recall = as.numeric(df2$recall)
df2$label = factor(df2$label, levels = c("Two-stage Classifier", "One-stage Classifier", "Random Guess") )
labels = c("Two-stage Classifier", "One-stage Classifier", "Random Guess") 

ggplot(df2, aes(x = recall, y = precision, colour = label, linetype = label)) +
  geom_line(size = 2) +
  scale_color_manual (values = c("black",  "grey20", "grey50"),  labels = labels) + 
  scale_linetype_manual(values = c("solid", "dotted", "dotdash"), labels = labels) +
  guides(color = guide_legend(title = "Classifier"), linetype = guide_legend(title = "Classifier")) +
  theme_bw() +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  theme(axis.text = element_text(size = 20, colour = "black"),
        axis.title = element_text(size = 28, colour = "black"),
        plot.title = element_text(size = 20, colour = "black")) +
  theme(legend.text=element_text(size= 26),
        legend.key.width = unit(3, "line"),
        legend.key.height = unit(2, "line"),
        legend.title=element_text(size= 24)) +theme(legend.position = c(0.4, 0.2)) +
  ylim (0,1)


ggsave("c1c2_vs_c1_precision_recall.pdf",
      dpi = DPI, width = WIDTH, height = WIDTH / 1.2, units = UNIT)
